/*******************************************************************************
 * AutoLock
 * 修改:   16-6-22 下午5:13
 *
 * Copyright (c) 2016. 广州市国迈科技有限公司安全私有云部 版权所有
 ******************************************************************************/
package com.taofuxn.admin.common.util;

import org.springframework.util.Assert;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;

@SuppressWarnings( {"LockAcquiredButNotSafelyReleased", "NullableProblems"})
public class AutoLock implements Lock, AutoCloseable {
    private Lock m_Lock;

    public AutoLock(Lock vLock) {
        this(vLock, true);
        return;
    }

    public AutoLock(Lock vLock, boolean vLockNow) {
        Assert.notNull(vLock);

        m_Lock = vLock;

        if(true == vLockNow)
            m_Lock.lock();

        return;
    }

    @Override
    public void close() {
        m_Lock.unlock();
        return;
    }

    @Override
    public void lock() {
        m_Lock.lock();
        return;
    }

    @Override
    public void lockInterruptibly() throws InterruptedException {
        m_Lock.lockInterruptibly();
        return;
    }

    @Override
    public boolean tryLock() {
        return m_Lock.tryLock();
    }

    @Override
    public boolean tryLock(long vTime, TimeUnit vUnit) throws InterruptedException {
        return m_Lock.tryLock(vTime, vUnit);
    }

    @Override
    public void unlock() {
        m_Lock.unlock();
        return;
    }

    @Override
    public Condition newCondition() {
        return m_Lock.newCondition();
    }
}
